package api;

import com.fasterxml.jackson.databind.ObjectMapper;
import model.Boke;
import model.BokeDao;
import model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

@WebServlet("/boke")
public class BokeServlet extends HttpServlet {

    private ObjectMapper objectMapper=new ObjectMapper();
    BokeDao bokeDao=new BokeDao();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //req.getParameter请求根据bokeid这个键值对的键key去取出值value
        String bokeId=req.getParameter("bokeId");

        if(bokeId==null)
        {
            //如果bokeId为空，说明没有qurey String ，请求访问的就是博客列表页
            List<Boke> bokeList=bokeDao.selectALL();
            //把这个bokelist链表（属组）转变为json字符串
            String respJson=objectMapper.writeValueAsString(bokeList);
            resp.setContentType("application/json;charset=utf8");
            //将json字符串返回到响应中
            resp.getWriter().write(respJson);
        }else{
            //反之如果bokeId不为空，说明有qurey String ，请求访问的就是博客详情页
            Boke boke=bokeDao.select(Integer.parseInt(bokeId));

            if(boke==null)
            {
                System.out.println("bokeId " +bokeId+"对应的博客不存在！！！");
            }
            String respJson=objectMapper.writeValueAsString(boke);
            resp.setContentType("application/json;charset=utf8");
            //将json字符串返回到响应中
            resp.getWriter().write(respJson);
            }
        }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        HttpSession session= req.getSession(false);

        if(session==null)
        {
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("当前未登录，无法发布博客");
            return;
        }

        User user=(User) session.getAttribute("user");
        if(user==null)
        {
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("当前未登录，无法发布博客");
            return;
        }

        req.setCharacterEncoding("utf8");
        String title=req.getParameter("title");
        String content=req.getParameter("content");
        if(title==null || "".equals(title) || content==null || "".equals(content))
        {
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("提交的数据有误，请重新输入！！！");
            return;
        }
        Boke boke=new Boke();
        boke.setTitle(title);
        boke.setContent(content);
        boke.setUserId(user.getUserId());
        boke.setPostTime(new Timestamp(System.currentTimeMillis() ));

        BokeDao bokeDao=new BokeDao();
        bokeDao.add(boke);
        // 跳转到博客列表页
        resp.sendRedirect("boke_list.html");
    }
}
